home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / pygtk / 2.0 / demos / stock_browser.py < prev    next >
Text File  |  2006-01-20  |  9KB  |  271 lines

  1. #!/usr/bin/env python
  2. '''Stock Item and Icon Browser
  3.  
  4. This source code for this demo doesn't demonstrate anything particularly
  5. useful in applications. The purpose of the "demo" is just to provide a
  6. handy place to browse the available stock icons and stock items.
  7. '''
  8. # pygtk version: Maik Hertha <maik.hertha@berlin.de>
  9.  
  10. import gobject
  11. import gtk
  12. import re
  13.  
  14. def id_to_macro(stock_id):
  15.     if stock_id == '':
  16.         return ''
  17.     if stock_id.startswith('gtk'):
  18.         # gtk-foo-bar -> gtk.STOCK_FOO_BAR
  19.         macro = 'gtk.STOCK' + \
  20.             re.sub('-([^-]+)', lambda m:('_' + m.group(1).upper()), stock_id[3:])
  21.  
  22.     else:
  23.         # demo-gtk-logo -> DEMO_GTK_LOGO as with custom icon-factories
  24.         macro = re.sub('([^-]+)-?', lambda m:('_' + m.group(1).upper()), stock_id)
  25.         macro = macro[1:]  # there is a leading '_' always
  26.  
  27.     return  macro
  28.  
  29.  
  30. class StockItemInfo(object):
  31.     def __init__(self, stock_id=''):
  32.         self.stock_id = stock_id
  33.         self.stock_item = None
  34.         self.small_icon = None
  35.         self.macro = id_to_macro(stock_id)
  36.         self.accel_str = ''
  37.  
  38. class StockItemDisplay(object):
  39.     def __init__(self):
  40.         self.type_label = None
  41.         self.macro_label = None
  42.         self.id_label = None
  43.         self.label_accel_label = None
  44.         self.icon_image = None
  45.  
  46.  
  47. def get_largest_size(stockid):
  48.     ''' Finds the largest size at which the given image stock id is
  49.         available. This would not be useful for a normal application.
  50.     '''
  51.     set = gtk.icon_factory_lookup_default(stockid)
  52.     best_size = gtk.ICON_SIZE_INVALID
  53.     best_pixels = 0
  54.  
  55.     sizes = set.get_sizes()
  56.     n_sizes = len(sizes)
  57.  
  58.     i = 0
  59.     while(i < n_sizes):
  60.         width, height = gtk.icon_size_lookup(sizes[i])
  61.  
  62.         if(width * height > best_pixels):
  63.             best_size = sizes[i]
  64.             best_pixels = width * height
  65.  
  66.         i += 1
  67.  
  68.     return best_size
  69.  
  70.  
  71. def macro_set_func_text(tree_column, cell, model, iter):
  72.     info = model.get_value(iter, 0)
  73.     cell.set_property("text", info.macro)
  74.  
  75. def id_set_func(tree_column, cell, model, iter):
  76.     info = model.get_value(iter, 0)
  77.     cell.set_property("text", info.stock_id)
  78.  
  79. def accel_set_func(tree_column, cell, model, iter):
  80.     info = model.get_value(iter, 0)
  81.     cell.set_property("text", info.accel_str)
  82.  
  83. def label_set_func(tree_column, cell, model, iter):
  84.     info = model.get_value(iter, 0)
  85.     cell.set_property("text", info.stock_item[1])
  86.  
  87.  
  88. class StockItemAndIconBrowserDemo(gtk.Window):
  89.     def __init__(self, parent=None):
  90.         gtk.Window.__init__(self)
  91.         try:
  92.             self.set_screen(parent.get_screen())
  93.         except AttributeError:
  94.             self.connect('destroy', lambda *w: gtk.main_quit())
  95.  
  96.         self.set_title(self.__class__.__name__)
  97.         self.set_default_size(-1, 500)
  98.         self.set_border_width(8)
  99.  
  100.         hbox = gtk.HBox(False, 8)
  101.         self.add(hbox)
  102.  
  103.         sw = gtk.ScrolledWindow()
  104.         sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
  105.         sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
  106.         hbox.pack_start(sw, False, False, 0)
  107.  
  108.         model = self.__create_model()
  109.         treeview = gtk.TreeView(model)
  110.         sw.add(treeview)
  111.  
  112.         column = gtk.TreeViewColumn()
  113.         column.set_title("Macro")
  114.  
  115.         cell_renderer = gtk.CellRendererPixbuf()
  116.         column.pack_start(cell_renderer, False)
  117.         column.set_attributes(cell_renderer, stock_id=1)
  118.  
  119.         cell_renderer = gtk.CellRendererText()
  120.         column.pack_start(cell_renderer, True)
  121.         column.set_cell_data_func(cell_renderer, macro_set_func_text)
  122.  
  123.         treeview.append_column(column)
  124.  
  125.         cell_renderer = gtk.CellRendererText()
  126.         treeview.insert_column_with_data_func(-1, "Label", cell_renderer, label_set_func)
  127.  
  128.         cell_renderer = gtk.CellRendererText()
  129.         treeview.insert_column_with_data_func(-1, "Accel", cell_renderer, accel_set_func)
  130.  
  131.         cell_renderer = gtk.CellRendererText()
  132.         treeview.insert_column_with_data_func(-1, "ID", cell_renderer, id_set_func)
  133.  
  134.         align = gtk.Alignment(0.5, 0.0, 0.0, 0.0)
  135.         hbox.pack_end(align, False, False, 0)
  136.  
  137.         frame = gtk.Frame("Selected Item")
  138.         align.add(frame)
  139.  
  140.         vbox = gtk.VBox(False, 8)
  141.         vbox.set_border_width(4)
  142.         frame.add(vbox)
  143.  
  144.         display = StockItemDisplay()
  145.         treeview.set_data("stock-display", display)
  146.  
  147.         display.type_label  = gtk.Label()
  148.         display.macro_label = gtk.Label()
  149.         display.id_label    = gtk.Label()
  150.         display.label_accel_label = gtk.Label()
  151.         display.icon_image  = gtk.Image(); # empty image
  152.  
  153.         vbox.pack_start(display.type_label, False, False, 0)
  154.         vbox.pack_start(display.icon_image, False, False, 0)
  155.         vbox.pack_start(display.label_accel_label, False, False, 0)
  156.         vbox.pack_start(display.macro_label, False, False, 0)
  157.         vbox.pack_start(display.id_label, False, False, 0)
  158.  
  159.         selection = treeview.get_selection()
  160.         selection.set_mode(gtk.SELECTION_SINGLE)
  161.  
  162.         selection.connect("changed", self.on_selection_changed)
  163.  
  164.         self.show_all()
  165.  
  166.     def __create_model(self):
  167.         store = gtk.ListStore(
  168.             gobject.TYPE_PYOBJECT,
  169.             gobject.TYPE_STRING)
  170.  
  171.         ids = gtk.stock_list_ids()
  172.         ids.sort()
  173.  
  174.         for data in ids:
  175.             info = StockItemInfo(stock_id=data)
  176.             stock_item = gtk.stock_lookup(data)
  177.  
  178.             if stock_item:
  179.                 info.stock_item = stock_item
  180.             else:
  181.                 # stock_id, label, modifier, keyval, translation_domain
  182.                 info.stock_item =('', '', 0, 0, '')
  183.  
  184.             # only show icons for stock IDs that have default icons
  185.             icon_set = gtk.icon_factory_lookup_default(info.stock_id)
  186.             if icon_set is None:
  187.                 info.small_icon = None
  188.             else:
  189.                 # See what sizes this stock icon really exists at
  190.                 sizes = icon_set.get_sizes()
  191.                 n_sizes = len(sizes)
  192.  
  193.                 # Use menu size if it exists, otherwise first size found
  194.                 size = sizes[0];
  195.                 i = 0;
  196.                 while(i < n_sizes):
  197.                     if(sizes[i] == gtk.ICON_SIZE_MENU):
  198.                         size = gtk.ICON_SIZE_MENU
  199.                         break
  200.                     i += 1
  201.  
  202.                 info.small_icon = self.render_icon(info.stock_id, size)
  203.  
  204.                 if(size != gtk.ICON_SIZE_MENU):
  205.                     # Make the result the proper size for our thumbnail
  206.                     w, h = gtk.icon_size_lookup(gtk.ICON_SIZE_MENU)
  207.  
  208.                     scaled = info.small_icon.scale_simple(w, h, 'bilinear')
  209.                     info.small_icon = scaled
  210.  
  211.             if info.stock_item[3] == 0:
  212.                 info.accel_str = ""
  213.             else:
  214.                 info.accel_str = \
  215.                     gtk.accelerator_name(info.stock_item[3], info.stock_item[2])
  216.  
  217.             iter = store.append()
  218.             store.set(iter, 0, info, 1, info.stock_id)
  219.  
  220.         return store
  221.  
  222.     def on_selection_changed(self, selection):
  223.         treeview = selection.get_tree_view()
  224.         display = treeview.get_data("stock-display")
  225.  
  226.         model, iter = selection.get_selected()
  227.         if iter:
  228.             info = model.get_value(iter, 0)
  229.  
  230.             if(info.small_icon and info.stock_item[1]):
  231.                 display.type_label.set_text("Icon and Item")
  232.  
  233.             elif(info.small_icon):
  234.                 display.type_label.set_text("Icon Only")
  235.  
  236.             elif(info.stock_item[1]):
  237.                 display.type_label.set_text("Item Only")
  238.  
  239.             else:
  240.                 display.type_label.set_text("???????")
  241.  
  242.             display.macro_label.set_text(info.macro)
  243.             display.id_label.set_text(info.stock_id)
  244.  
  245.             if(info.stock_item[1]):
  246.                 s = "%s %s" % (info.stock_item[1], info.accel_str)
  247.                 display.label_accel_label.set_text_with_mnemonic(s)
  248.  
  249.             else:
  250.                 display.label_accel_label.set_text("")
  251.  
  252.             if(info.small_icon):
  253.                 display.icon_image.set_from_stock(info.stock_id,
  254.                                       get_largest_size(info.stock_id))
  255.             else:
  256.                 display.icon_image.set_from_pixbuf(None)
  257.  
  258.         else:
  259.             display.type_label.set_text("No selected item")
  260.             display.macro_label.set_text("")
  261.             display.id_label.set_text("")
  262.             display.label_accel_label.set_text("")
  263.             display.icon_image.set_from_pixbuf(None)
  264.  
  265. def main():
  266.     StockItemAndIconBrowserDemo()
  267.     gtk.main()
  268.  
  269. if __name__ == '__main__':
  270.     main()
  271.